package com.ruoyi.app.controller.order.mapper;

import com.ruoyi.common.core.dao.BaseMapper;
import com.ruoyi.common.entity.DO.order.ShopPetrolOrderDO;
import com.ruoyi.common.entity.VO.PayVO;
import com.ruoyi.common.entity.VO.order.ShopOrderSearchVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;

public interface IShopPetrolOrderMapper extends BaseMapper<ShopPetrolOrderDO> {

    List<ShopPetrolOrderDO> searchList(ShopOrderSearchVO shopOrderSearchVO);

    ShopPetrolOrderDO getDetail(ShopOrderSearchVO shopOrderSearchVO);

    @Select("SELECT order_sn orderSn,pay_sn paySn,pay_cost payCost,pay_way payWay FROM shop_petrol_order where order_sn=#{orderSn}")
    PayVO getPayInfo(String orderSn);

    @Select("SELECT id,product_id productId,product_sn productSn,product_name productName,product_quantity productQuantity,member_id memberId,shop_id shopId,order_sn orderSn,pay_cost payCost,order_cost orderCost,gift_credit giftCredit,pay_status payStatus,product_attr_name productAttrName FROM shop_petrol_order where pay_sn=#{paySn}")
    ShopPetrolOrderDO getDetailByPaySn(String paySn);

    @Update("UPDATE shop_petrol_order SET pay_status=1,order_status=3,pay_way=#{payWay},pay_time=sysdate() WHERE id=#{orderId}")
    int paySuccessHandle(@Param("orderId") Long orderId, @Param("payWay") Integer payWay);

    @Update("UPDATE shop_petrol_order SET gift_credit=0 WHERE id=#{orderId}")
    int resetGiftCredit(Long orderId);

    @Select("SELECT sum(discount_amount) FROM shop_petrol_order WHERE member_id=#{memberId} and pay_status=1 and order_status=3 and DATE_FORMAT(create_time, '%Y%m')=#{yearMonth} group by member_id")
    BigDecimal getTotalDiscountAmountByMonth(@Param("memberId") long memberId, @Param("yearMonth") String yearMonth);

    @Select("SELECT sum(pay_cost) FROM shop_petrol_order WHERE member_id=#{memberId} and pay_status=1 and order_status=3 and DATE_FORMAT(create_time, '%Y%m')=#{yearMonth} group by member_id")
    BigDecimal getTotalPayCostByMonth(@Param("memberId") long memberId, @Param("yearMonth") String yearMonth);
}
